// 快照沙箱-极简版 
class SnapshotSandBox {
    windowSnapshot = {};
    modifyPropsMap = {};
    active() {
        // 保存window对象上所有属性的状态
        for (const prop in window) {
            this.windowSnapshot[prop] = window[prop];
        }
        // 恢复上一次在运行该微应用的时候所修改过的window上的属性
        Object.keys(this.modifyPropsMap).forEach((prop) => {
            window[prop] = this.modifyPropsMap[prop];
        });
    }
    inactive() {
        for (const prop in window) {
            if (window[prop] !== this.windowSnapshot[prop]) {
                // 记录修改了window上的哪些属性
                this.modifyPropsMap[prop] = window[prop];
                // 将window上的属性状态还原至微应用运行之前的状态
                window[prop] = this.windowSnapshot[prop];
            }
        }
    }
}
// 验证:
let snapshotSandBox = new SnapshotSandBox();
snapshotSandBox.active();
window.city = "Beijing";
console.log("window.city-01:", window.city);
snapshotSandBox.inactive();
console.log("window.city-02:", window.city);
snapshotSandBox.active();
console.log("window.city-03:", window.city);
snapshotSandBox.inactive();

//输出：
//window.city-01: Beijing
//window.city-02: undefined
//window.city-03: Beijing
